package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.common.MessageConst;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/4/6 17:12
* @Version : 1.0
* @Description : 订单业务接口实现类
*/
@Service
@Transactional
@Slf4j
public class OrderServiceImpl implements OrderService {
    //依赖注入orderdao
    @Autowired
    private OrderDao orderDao;
    //依赖注入ordersettingsdao
    @Autowired
    private OrderSettingDao orderSettingDao;
    //依赖注入memberdao
    @Autowired
    private MemberDao memberDao;

    /**
     * 保存预约
     * @param map
     * @return
     */
    @Override
    public Result addOrder(Map<String, String> map) {
        try {
            //判断预约日期是否设置预约
            Date orderDate = DateUtils.parseString2Date(map.get("orderDate"));
            OrderSetting orderSetting= orderSettingDao.findByOrderDate(orderDate);
            //判断预约设置情况
            if (orderSetting==null){
                return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
            }

            //判断预约日期是否约满
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if (reservations>=number){
                return new Result(false,MessageConst.ORDER_FULL);
            }

            //判断用户是否是会员
            Member member= memberDao.findByTelephone(map.get("telephone"));
            int setmealId= Integer.parseInt(map.get("setmealId"));//套餐ID
            //判断会员用户是否已经预约
            if (member!=null){
                //查询是否预约
                Order order = new Order(null,member.getId(),orderDate,null,null,setmealId);
                List<Order> orderList= orderDao.findByCondition(order);
                if (orderList!=null&&orderList.size()>0){
                    return new Result(false,MessageConst.HAS_ORDERED);
                }
            }else {
                //不是会员 自动注册成会员
                member=new Member();
                member.setPhoneNumber(map.get("telephone"));//手机号
                member.setRegTime(new Date());//注册日期
                //调用memberdao 方法添入数据库
                memberDao.add(member);
            }

            //更新会员信息
            member.setName(map.get("name"));
            member.setSex(map.get("sex"));
            member.setIdCard(map.get("idCard"));
            memberDao.updata(member);

            //保存体检预约
            Order order = new Order(null,member.getId(),orderDate,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,setmealId);
            //调用orderDAO 方法
            orderDao.addOrder(order);

            //更新体检预约
            orderSetting.setReservations(orderSetting.getReservations()+1);
            orderSettingDao.update(orderSetting);

            return new Result(true,MessageConst.ORDER_SUCCESS,order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约失败");
        }
    }

    /**
     * 展示预约成功的信息
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
        //调用orderdao 方法
        Map map= orderDao.findById4Detail(id);
        try {
            //使用工具类 将预约日期的日期格式转成字符串格式
            String orderDate = DateUtils.parseDate2String((Date) map.get("orderDate"));
            //将生成的新日期格式 存入map集合中
            map.put("orderDate",orderDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
}
